from flask import Blueprint, jsonify, request
import requests
import json
import logging
from app.utils.AESC import AESCrypto_Yzdyh
import time

yzdyh_bp = Blueprint('yzdyh', __name__, url_prefix='/yzdyh')

@yzdyh_bp.route('/todoTg', methods=['GET'])
def todoTg():
    date = request.args.get('date')
    crypto = AESCrypto_Yzdyh()
    url = f"https://service.canalmuseum.net:8443/apiA/booking/getBookingData3?openid=oZiV06-dCtvKL5Xv3LOF6FoKdCTg&date={date}";
    # 发送网络请求
    headers = {
        "openid": "oZiV06-dCtvKL5Xv3LOF6FoKdCTg",
        "date": date,
        "token": "a11ef232-c76a-4ae1-b899-1f6dee89cd75",
        "sign": crypto.getptSign(),
        "referer": "https://servicewechat.com/...",
        "x-web-xhr": "1",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "cross-site",
        "connection": "keep-alive",
        "host": "service.canalmuseum.net:8443"
    }
    try:
        # 发送GET请求
        response = requests.get(url, headers=headers)
        # 检查响应状态
        if response.status_code == 200:
            print(f"body={response.text}")
            return jsonify({
                "status": "success",
                "data": response.json()  # 假设返回的是 JSON
            })
        else:
            print(f"请求失败，状态码: {response.status_code}")
            return jsonify({
                "status": "error",
                "code": response.status_code,
                "message": "API 请求失败"
            }), 400
    except Exception as e:
        print(f"请求发生异常: {str(e)}")
        return jsonify({
            "status": "error",
            "message": f"请求异常: {str(e)}"
        }), 500  # 返回 HTTP 500 状态码



@yzdyh_bp.route('/addOrder', methods=['GET'])
def addOrder():
    crypto = AESCrypto_Yzdyh()
    url = "https://service.canalmuseum.net:8443/apiA/booking/2";
    # 请求配置
    headers = {
        "Host": "service.canalmuseum.net:8443",
        "Connection": "keep-alive",
        "Content-Type": "application/json",
        "xweb_xhr": "1",
        "openid": "oZiV06-dCtvKL5Xv3LOF6FoKdCTg",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c33)XWEB/13639",
        "Authorization": "bearer undefined",
        "token": "a11ef232-c76a-4ae1-b899-1f6dee89cd75",
        "sign": crypto.getDdSign(),
        "Accept": "*/*",
        "Sec-Fetch-Site": "cross-site",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Dest": "empty",
        "Referer": "https://servicewechat.com/wx281a8b44bb2dc0d6/101/page-frame.html",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9"
    }
    params = {
        "adultList": [
            {"idNo": "622627199711264610", "name": "何荣", "idType": 0, "active": True, "isMain": 0}
        ],
        "childList": [],
        "bookingTime": "2025-05-22 00:00:00",
        "type": 1,
        "chooseTime": "09:00-12:00",
        "state": 1,
        "timeRange": 1,
        "dateManageId": 1208,
        "mobile": "17693240068",
        "openid": "oZiV06-dCtvKL5Xv3LOF6FoKdCTg"
    }
    try:
        response = requests.post(
            url,
            headers=headers,
            data=json.dumps(params),
        );
        # 检查响应状态
        if response.status_code == 200:
            print(f"body={response.text}")
            return jsonify({
                "status": "success",
                "data": response.json()  # 假设返回的是 JSON
            })
        else:
            print(f"请求失败，状态码:{response.status_code} {response.text}")
            return jsonify({
                "status": "error",
                "code": response.status_code,
                "message": "API 请求失败"
            }), 400
    except Exception as e:
        print(f"请求发生异常: {str(e)}")
        return jsonify({
            "status": "error",
            "message": f"请求异常: {str(e)}"
        }),
